import { _decorator, Component, Node, RenderRoot2D, game } from 'cc'
import DataManager from './DataManager'
import Singleton from '../Base/Singleton'
import { DrawManager } from '../Scripts/UI/DrawManager'
import { createUINode } from '../Utils'
const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = FaderManager
 * DateTime = Sun Jul 31 2022 18:25:01 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = FaderManager.ts
 * FileBasenameNoExtension = FaderManager
 * URL = db://assets/Runtime/FaderManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('FaderManager')
export class FaderManager extends Singleton {
  static get Instance() {
    return super.GetInstance<FaderManager>()
  }
  private _fader: DrawManager = null

  get fader() {
    if (this._fader !== null) {
      return this._fader
    }
    const root = createUINode()
    root.addComponent(RenderRoot2D)
    const faderNode = createUINode()
    faderNode.setParent(root)
    this._fader = faderNode.addComponent(DrawManager)
    this._fader.init()
    game.addPersistRootNode(root)

    return this._fader
  }

  fadeIn(duration: number) {
    return this.fader.fadeIn(duration)
  }
  fadeOut(duration: number) {
    return this.fader.fadeOut(duration)
  }
  mask() {
    return this.fader.mask()
  }
}
